<!Doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>Vue2.0 新手入门 &#8212; 从环境搭建到发布 | 菜鸟教程</title>

  <meta name='robots' content='max-image-preview:large' />
<link rel='stylesheet' id='classic-theme-styles-css' href='../0/classic-themes.min.css?ver=6.2' type='text/css' media='all' />
<link rel="canonical" href="../w3cnote/vue2-start-coding.html" />
<meta name="keywords" content="Vue2.0 新手入门 &#8212; 从环境搭建到发布">
<meta name="description" content="Jinkey原创感谢 showonne、yubang 技术指导Demo 地址: http://demo.jinkey.io/vue2源码:https://github.com/Jinkeycode/vue2-example Vue2 教程：https://www.runoob.com/vue2/vue-tutorial.html  什么是 Vue Vue 是一个前端框架，特点是数据绑定  比如你改变一个输入框 Input 标签的值，会自..">
		
	<link rel="shortcut icon" href="https://static.runoob.com/images/favicon.ico">
	<link rel="stylesheet" href="../0/style.css?v=1.170" type="text/css" media="all" />	
	<link rel="stylesheet" href="../0/font-awesome.min.css" media="all" />	
  <!--[if gte IE 9]><!-->
  <script src=""></script>
  <!--<![endif]-->
  <!--[if lt IE 9]>
     <script src=""></script>
     <script src=""></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="https://static.runoob.com/images/icon/mobile-icon.png"/>
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
</head>
<body>

<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" placeholder="搜索……" name="s" autocomplete="off">
      
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="../">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        
    <div class="col search search-desktop last">
      <div class="search-input" >
      <form action="//www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" placeholder="搜索……"  autocomplete="off" style="height: 44px;">
      </form>
      
      </div>
    </div>
  </div>
</div>



<!-- 导航栏 -->
<div class="container navigation">
    <div class="row">
        <div class="col nav">
            

                        <ul class="pc-nav" id="note-nav">
                <li><a href="../">首页</a></li>
                <li><a href="../w3cnote">笔记首页</a></li>
                <li><a href="../w3cnote/android-tutorial-intro.html" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" title="ES6 教程">ES6 教程</a></li>
                <li><a href="../w3cnote/ten-sorting-algorithm.html" title="排序算法">排序算法</a></li>
                <li><a href="../w3cnote/hadoop-tutorial.html" title="Hadoop 教程">Hadoop</a></li>
                <li><a href="../w3cnote/zookeeper-tutorial.html" title="Zookeeper 教程">Zookeeper</a></li>
                <li><a href="../w3cnote/verilog-tutorial.html" title="Verilog 教程">Verilog</a></li>
                <li><a href="../w3cnote_genre/code" title="编程技术">编程技术</a></li> 
                <li><a href="../w3cnote_genre/coderlife" title="程序员人生">程序员人生</a></li>
                
                <!--<li><a href="javascript:;" class="runoob-pop">登录</a></li>
                
                
                        <li>
                <a style="font-weight:bold;" href="../linux/linux-tutorial.html#yunserver" target="_blank" onclick="_hmt.push(['_trackEvent', 'aliyun', 'click', 'aliyun'])" title="kkb">云服务器</a>
                </li>
                <li><a href="http://gk.link/a/104mQ" target="_blank" style="font-weight: bold;"onclick="_hmt.push(['_trackEvent', '极客时间', 'click', 'jike'])" title="我的圈子">极客时间</a></li>
            
                
                <li><a target="_blank" href="../shoppinglist" rel="nofollow">知识店铺</a></li> 
        -->
            </ul>
                        
              
            <ul class="mobile-nav">
                <li><a href="../w3cnote">首页</a></li>
                <li><a href="../w3cnote_genre/android" target="_blank" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" target="_blank" title="ES6 教程">ES6</a></li>
                <li><a href="../w3cnote_genre/joke" target="_blank" title="程序员笑话">逗乐</a></li>
                
                <a href="javascript:void(0)" class="search-reveal">Search</a> 
            </ul>
            
        </div>
    </div>
</div>


<!--  内容  -->
<div class="container main">
	<div class="row">

		<div class="col middle-column big-middle-column">
	 			<div class="article">
			<div class="article-heading">
				<h2>Vue2.0 新手入门 &#8212; 从环境搭建到发布</h2>				<h3><em>分类</em> <a href="../w3cnote_genre/code" title="编程技术" >编程技术</a> </h3>
			</div>
			<div class="article-body note-body">
				<div class="article-intro">
					<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476689796-1178-854231-d682f0bb3d8eee9c.png"></p>
<blockquote><p>Jinkey原创<br>感谢 <a href="https://github.com/showonne" target="_blank" rel="noopener">showonne</a>、<a href="https://github.com/yubang" target="_blank" rel="noopener">yubang</a> 技术指导<br><strong>Demo 地址:</strong><br> <a href="http://demo.jinkey.io/vue2" target="_blank" rel="noopener">http://demo.jinkey.io/vue2</a><br><strong>源码:</strong><br><a href="https://github.com/Jinkeycode/vue2-example" target="_blank" rel="noopener">https://github.com/Jinkeycode/vue2-example</a></p>
<p>Vue2 教程：<a href="../vue2/vue-tutorial.html" rel="noopener" target="_blank">https://www.runoob.com/vue2/vue-tutorial.html</a></p>
</blockquote><hr>
<h2>什么是 Vue</h2>
<p>Vue 是一个前端框架，特点是<br><strong>数据绑定</strong></p>
<blockquote>
<p>比如你改变一个输入框 Input 标签的值，会<strong>自动同步</strong>更新到页面上其他绑定该输入框的组件的值<br>￼<br></p>
<img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476690131-6882-854231-4a2d31db6bc25a1a.png"></p>
</blockquote>
<h3>组件化</h3>
<blockquote><p>页面上小到一个按钮都可以是一个单独的文件.vue，这些小组件直接可以像乐高积木一样通过互相引用而组装起来</p></blockquote>
<p>
<img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476690177-7476-854231-5eeceaec4c7e80ed.jpg"></p>
<hr>
<h2>Vue2.0 推荐开发环境</h2>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2016/10/1476690217-1155-854231-1d2947692fba4957-1-1.png"></p>
<p>Homebrew 1.0.6(Mac)、Node.js 6.7.0、npm 3.10.3、webpack 1.13.2、vue-cli 2.4.0、Atom 1.10.2</p>
<hr>
<h2>环境安装</h2>

<h3>Mac OS系统安装 brew</h3>
<p>打开终端运行以下命令: </p>
<pre>/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
</pre>
<h3>Mac OS系统安装 nodejs</h3>
<pre>brew install nodejs</pre>
<p>用 npm install npm@3.10.3 更新 npm 版本报错:</p>
<blockquote><p>(node:42) fs: re-evaluating native module sources is not supported.</p></blockquote>
<p>解决办法:<br>在官网下载6.70的安装包再安装一次(刚刚相当于帮你配置好环境变量，现在再安装一次升级到最新的 npm)</p>
<blockquote><ul>
<li>好像以前官网的安装包不会自动配置环境变量的，由于我电脑上之前安装过 nodejs 所以环境变量已经配置好了，不知道现在的安装包会不会自动配置环境变量。</li>
</ul></blockquote>
<p>Windows 下直接下载安装包即可</p>
<p>linux 下可以使用 apt-get（ubuntu） 或 yum（centos） 命令安装。</p>
<p>具体可参考：<a href="../nodejs/nodejs-install-setup.html" target="_blank" rel="noopener">http://www.runoob.com/nodejs/nodejs-install-setup.html</a></p>
<h3>获取nodejs模块安装目录访问权限
</h3>
<pre>sudo chmod -R 777 /usr/local/lib/node_modules/</pre>
<p>安装淘宝镜像</p>
<p>大家都知道国内直接使用 npm 的官方镜像是非常慢的，这里推荐使用淘宝 NPM 镜像。</p>
<pre>
npm install -g cnpm --registry=https://registry.npmmirror.com
</pre>
<p>这样就可以使用 cnpm 命令来安装模块了：</p>
<pre>
cnpm install [name]
</pre>
<h3>安装webpack</h3>
<pre>cnpm install webpack -g</pre>
<h3>安装vue脚手架</h3>
<pre>
npm install vue-cli -g
</pre>
<h3>在硬盘上找一个文件夹放工程用的，在终端中进入该目录</h3>
<pre>
cd 目录路径
</pre>
<h3>根据模板创建项目</h3>
<blockquote><p>vue init webpack-simple 工程名字&lt;工程名字不能用中文&gt;<br>或者创建 vue1.0 的项目<br>vue init webpack-simple#1.0 工程名字&lt;工程名字不能用中文&gt;</p></blockquote>
<p>会有一些初始化的设置，如下输入:<br><code>Target directory exists. Continue? (Y/n)</code>直接回车默认(然后会下载 vue2.0模板，这里可能需要连代理)<br><code>Project name (vue-test)</code>直接回车默认<br><code>Project description (A Vue.js project)</code>  直接回车默认<br><code>Author</code> 写你自己的名字</p>
<h3>cd 命令进入创建的工程目录</h3>
<p>工程目录如图所示:</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476690721-1278-854231-ea029fb82e865d5d.jpg"></p>
<hr>
<h2>安装项目依赖</h2>
<p>一定要从官方仓库安装，npm 服务器在国外所以这一步安装速度会很慢。</p>
<pre>
npm install
</pre>
<p><strong>不要从国内镜像cnpm安装</strong>(会导致后面缺了很多依赖库)</p>
<pre>
cnpm install
</pre>
<h3>安装 vue 路由模块<code>vue-router</code>和网络请求模块<code>vue-resource</code>
</h3>
<pre>
cnpm install vue-router vue-resource --save
</pre>
<h3>启动项目</h3>
<pre>npm run dev</pre>
<h3>填坑(以下坑可能由于 vue2.0 导致其他相关编译打包工具没更新导致的)</h3>
<blockquote><p><strong>【重点】后来发现这些坑是由于 npm 不是最新的版本3.10.2， 用 npm 3.9.5就会出现以下坑</strong><br>解决办法: 请运行以下命令<br><strong>npm update -g</strong></p></blockquote>
<p>报错</p>
<pre>
Error: Cannot find module 'opn'
Error: Cannot find module 'webpack-dev-middleware'
Error: Cannot find module 'express'
Error: Cannot find module 'compression'
Error: Cannot find module 'sockjs'
Error: Cannot find module 'spdy'
Error: Cannot find module 'http-proxy-middleware'
Error: Cannot find module 'serve-index'
</pre>
<p>如果你用的是老版本的 vue-cli 还可能报其他错误，需要更新一下 vue-cli</p>
<pre>npm update vue-cli</pre>
<p>然后可以查看一下当前全局 vue-cli 的版本</p>
<pre>
npm view vue-cli
</pre>
<p>安装一下这个依赖到工程开发环境</p>

<pre>cnpm install opn --save-dev
cnpm install webpack-dev-middleware --save-dev
cnpm install express --save-dev
cnpm install compression --save-dev
cnpm install sockjs --save-dev
cnpm install spdy --save-dev
cnpm install http-proxy-middleware --save-dev
cnpm install serve-index --save-dev
cnpm install connect-history-api-fallback --save-dev</pre>

<p>再启动项目，报错</p>
<pre>ERROR in ./src/main.js
Module build failed: Error: Cannot find module 'babel-runtime/helpers/typeof'
at Function.Module._resolveFilename (module.js:440:15)
at Function.Module._load (module.js:388:25)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object.&lt;anonymous&gt; (/Volumes/MacStorage/Coding/Web/vue-test/node_modules/.6.17.0@babel-core/lib/transformation/file/index.js:6:16)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
@ multi main
ERROR in ./~/.2.1.0-beta.8@webpack-dev-server/client/socket.js
Module not found: Error: Can't resolve 'sockjs-client' in '/Volumes/MacStorage/Coding/Web/vue-test/node_modules/.2.1.0-beta.8@webpack-dev-server/client'
@ ./~/.2.1.0-beta.8@webpack-dev-server/client/socket.js 1:13-37
@ ./~/.2.1.0-beta.8@webpack-dev-server/client?http://localhost:8080</pre>
<p>安装一下 babel-runtime</p>
<pre>cnpm install babel-helpers --save-dev</pre>
<p>启动项目，再次报错</p>
<pre>
Module build failed: Error: Cannot find module 'babel-helpers'
Module build failed: Error: Cannot find module 'babel-traverse'
Module build failed: Error: Cannot find module 'json5'
Module build failed: Error: Cannot find module 'babel-generator'
Module build failed: Error: Cannot find module 'detect-indent'
Module build failed: Error: Cannot find module 'jsesc'
</pre>
<p>找不到依赖那就再安装一下</p>
<pre>
cnpm install babel-helpers --save-dev
cnpm install babel-traverse --save-dev
cnpm install json5 --save-dev
...不写了，请把全部把旧的环境全部清除，更新到最新版本
</pre>
<hr>
<h2>解决办法概述</h2>
<p>遇到</p>
<pre>Module build failed: Error: Cannot find module '模块名'</pre>
<p>那就安装</p>
<pre>
cnpm install 模块名 --save-dev(关于环境的，表现为npm run dev 启动不了)
cnpm install 模块名 --save(关于项目的，比如main.js，表现为npm run dev 成功之后控制台报错)
比如escape-string-regexp、strip-ansi、has-ansi、is-finite、emojis-list
</pre>
<h3>终于可以启动项目了</h3>
<p>输入完命令会自动启动浏览器，如果默认打开 IE 不行</p>
<pre>
npm run dev
</pre>
<p>自动启动浏览器就会看到这个界面了。<br></p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691152-5858-854231-1f208cb27858c8ec.png"></p>
<hr>
<h2>开始 Vue 之旅</h2>
<h3>打开 IDE</h3><p>
推荐 Atom 打开项目，需要安装 Vue 语法高亮的插件</p>
<img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691201-9307-854231-bc9565e738d4d41e.jpg"></p>
<h3>使用官网文档学习基础</h3>
<p>我们来看官网的一个例子，(中文文档请自行上网搜索)</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691245-3231-854231-00c7dced26f9c972.jpg"></p>
<p>打开 工程目录下的 App.vue</p><p>
template 写 html，script写 js，style写样式

</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691280-8912-854231-22ac84c532a7be20.jpg"></p>
<p>为了方便叙述，我们把官网例子写在同一个组件内<br>这里有两个坑:<br><strong>第一</strong>。一个组件下只能有一个并列的 div，可以这么写，所以复制官网示例的时候只要复制 div 里面的内容就好。<br></p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691313-1110-854231-b219f2ef37474fe1.jpg"></p><p>但是不能这样写:￼</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691340-5135-854231-4343b062c2b504aa.jpg"></p><p><strong>第二</strong>。数据要写在 return 里面而不是像文档那样子写<br></p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691387-6051-854231-5da5ffe18910e72f.jpg"></p>
<p>错误的写法:</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691429-8316-854231-9b78747081a19c39.jpg"></p>
<p>这样子可以自己啃完官网文档组件之前的部分了。<br></p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691463-8515-854231-55342acee9153c98.png"></p>
<hr>
<h2>来玩玩组件</h2>
<p> 前面讲得基本上都是各种常用组件的数据绑定，下面还得说说的是 Vue 的组件的使用。<br>在工程目录<code>/src</code>下创建<code>component</code>文件夹，并在<code>component</code>文件夹下创建一个 <code>firstcomponent.vue</code>并写仿照 App.vue 的格式和前面学到的知识写一个组件。</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">template</span><span class="hl-brackets">&gt;</span><span class="hl-code">
  </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">div</span><span class="hl-code"> </span><span class="hl-var">id</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">firstcomponent</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
    </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">h1</span><span class="hl-brackets">&gt;</span><span class="hl-code">I am a title.</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">h1</span><span class="hl-brackets">&gt;</span><span class="hl-code">
    </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">a</span><span class="hl-brackets">&gt;</span><span class="hl-code"> written by {{ author }} </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">a</span><span class="hl-brackets">&gt;</span><span class="hl-code">
  </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">div</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">template</span><span class="hl-brackets">&gt;</span><span class="hl-code">
 
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">script</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">text/javascript</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
export default {
  data () {
    return {
      author: &quot;微信公众号 jinkey-love&quot;
    }
  }
}
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span><span class="hl-code">
 
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">style</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">style</span><span class="hl-brackets">&gt;</span></div>
</div>
</div>
<p> duang... 不能按官网文档那样子叫我做就做，我得先试试再告诉你，我做完效果是这样子的，希望观众做完也是这样子。(迷之微笑 )<br></p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691520-7423-854231-bb5a6ba776b17a94.jpg"></p><p>然后在 App.vue 使用组件 ( 因为在 <code>index.html</code> 里面定义了&lt;div id="app"&gt;&lt;/div&gt;所以就以这个组件作为主入口，方便 )<br><strong>第一步，引入</strong>。在<code>&lt;script&gt;&lt;/script&gt;</code>标签内的第一行写</p>
<pre>
import firstcomponent from './component/firstcomponent.vue'
</pre><p><strong>第二步，注册</strong>。在<code>&lt;script&gt;&lt;/script&gt;</code>标签内的 data 代码块后面加上 components: { firstcomponent }。<strong>记得中间加英文逗号!!!</strong></p>
<pre>
export default {
  data () {
    return {
      msg: 'Hello Vue!'
    }
  },
  components: { firstcomponent }
}
</pre>
<p><strong>第三步，使用</strong>。<br>在<code>&lt;template&gt;&lt;/template&gt;</code>内加上&lt;firstcomponent&gt;&lt;/firstcomponent&gt;</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">template</span><span class="hl-brackets">&gt;</span><span class="hl-code">
  </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">div</span><span class="hl-code"> </span><span class="hl-var">id</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">app</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
    </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">img</span><span class="hl-code"> </span><span class="hl-var">decoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">async</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">src</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">./assets/logo.png</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
    </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">h1</span><span class="hl-brackets">&gt;</span><span class="hl-code">{{ msg }}</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">h1</span><span class="hl-brackets">&gt;</span><span class="hl-code">
    </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">firstcomponent</span><span class="hl-brackets">&gt;</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">firstcomponent</span><span class="hl-brackets">&gt;</span><span class="hl-code">
  </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">div</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">template</span><span class="hl-brackets">&gt;</span></div>
</div>
</div>
<p>完成后的代码：</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691599-4231-854231-9151330e194eef69.jpg"></p>
<p>这时候看看浏览器上的 <code>http://localhost:8080/</code> 页面(之前打开过就会自动刷新)，如果你没看到效果是因为你没有对 <code>App.vue</code> 和 <code>firstcomponent.vue</code> 进行保存操作，保存后页面会自动刷新。<br></p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691628-6951-854231-03fd1e86031881e6.png"></p><hr>
<h2>使用路由搭建单页应用</h2>
<p>之前已经通过命令安装了vue-router</p>
<pre>
cnpm install vue-router --save
</pre>
<p>在webpack.config.js加入别名</p>
<pre>
resolve: {
&nbsp;&nbsp;&nbsp;&nbsp;alias: {vue: 'vue/dist/vue.js'}
}
</pre>
<p>为什么要加 alias 配置项？其作用可以在文档中有相应的描述:<br></p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691751-9448-854231-3fb25f5fd8b8c8d2.jpg"></p>
<p>修改完之后的<code>webpack.config.js</code>是这样子的:</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">path</span><span class="hl-code"> = </span><span class="hl-identifier">require</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">path</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">webpack</span><span class="hl-code"> = </span><span class="hl-identifier">require</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">webpack</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-identifier">module</span><span class="hl-code">.</span><span class="hl-identifier">exports</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-identifier">entry</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">./src/main.js</span><span class="hl-quotes">'</span><span class="hl-code">,
  </span><span class="hl-identifier">output</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">path</span><span class="hl-code">: </span><span class="hl-identifier">path</span><span class="hl-code">.</span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-identifier">__dirname</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">./dist</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">,
    </span><span class="hl-identifier">publicPath</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">/dist/</span><span class="hl-quotes">'</span><span class="hl-code">,
    </span><span class="hl-identifier">filename</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">build.js</span><span class="hl-quotes">'</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">resolveLoader</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">root</span><span class="hl-code">: </span><span class="hl-identifier">path</span><span class="hl-code">.</span><span class="hl-identifier">join</span><span class="hl-brackets">(</span><span class="hl-identifier">__dirname</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">node_modules</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">,
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">module</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">loaders</span><span class="hl-code">: </span><span class="hl-brackets">[</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-identifier">vue</span><span class="hl-code">$/,
        </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">vue</span><span class="hl-quotes">'</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">,
      </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-identifier">js</span><span class="hl-code">$/,
        </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">babel</span><span class="hl-quotes">'</span><span class="hl-code">,
        </span><span class="hl-identifier">exclude</span><span class="hl-code">: /</span><span class="hl-identifier">node_modules</span><span class="hl-code">/
      </span><span class="hl-brackets">}</span><span class="hl-code">,
      </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-brackets">(</span><span class="hl-identifier">png</span><span class="hl-code">|</span><span class="hl-identifier">jpg</span><span class="hl-code">|</span><span class="hl-identifier">gif</span><span class="hl-code">|</span><span class="hl-identifier">svg</span><span class="hl-brackets">)</span><span class="hl-code">$/,
        </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">file</span><span class="hl-quotes">'</span><span class="hl-code">,
        </span><span class="hl-identifier">query</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
          </span><span class="hl-identifier">name</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">[name].[ext]?[hash]</span><span class="hl-quotes">'</span><span class="hl-code">
        </span><span class="hl-brackets">}</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-brackets">]</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">resolve</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">alias</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-identifier">vue</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">vue/dist/vue.js</span><span class="hl-quotes">'</span><span class="hl-brackets">}</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">devServer</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">historyApiFallback</span><span class="hl-code">: </span><span class="hl-reserved">true</span><span class="hl-code">,
    </span><span class="hl-identifier">noInfo</span><span class="hl-code">: </span><span class="hl-reserved">true</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">devtool</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">#eval-source-map</span><span class="hl-quotes">'</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">process</span><span class="hl-code">.</span><span class="hl-identifier">env</span><span class="hl-code">.</span><span class="hl-identifier">NODE_ENV</span><span class="hl-code"> === </span><span class="hl-quotes">'</span><span class="hl-string">production</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-identifier">module</span><span class="hl-code">.</span><span class="hl-identifier">exports</span><span class="hl-code">.</span><span class="hl-identifier">devtool</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">#source-map</span><span class="hl-quotes">'</span><span class="hl-code">
  </span><span class="hl-comment">//</span><span class="hl-comment"> </span><span class="hl-url">http://vue-loader.vuejs.org/en/workflow/production.html</span><span class="hl-comment"></span><span class="hl-code">
  </span><span class="hl-identifier">module</span><span class="hl-code">.</span><span class="hl-identifier">exports</span><span class="hl-code">.</span><span class="hl-identifier">plugins</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">module</span><span class="hl-code">.</span><span class="hl-identifier">exports</span><span class="hl-code">.</span><span class="hl-identifier">plugins</span><span class="hl-code"> || </span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">concat</span><span class="hl-brackets">(</span><span class="hl-brackets">[</span><span class="hl-code">
    </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">webpack</span><span class="hl-code">.</span><span class="hl-identifier">DefinePlugin</span><span class="hl-brackets">(</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-quotes">'</span><span class="hl-string">process.env</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">NODE_ENV</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">&quot;production&quot;</span><span class="hl-quotes">'</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">,
    </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">webpack</span><span class="hl-code">.</span><span class="hl-identifier">optimize</span><span class="hl-code">.</span><span class="hl-identifier">UglifyJsPlugin</span><span class="hl-brackets">(</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">compress</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">warnings</span><span class="hl-code">: </span><span class="hl-reserved">false</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">
  </span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>再按之前的方法写一个组件 secondcomponent.vue</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">template</span><span class="hl-brackets">&gt;</span><span class="hl-code">
  </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">div</span><span class="hl-code"> </span><span class="hl-var">id</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">secondcomponent</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
    </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">h1</span><span class="hl-brackets">&gt;</span><span class="hl-code">I am another page</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">h1</span><span class="hl-brackets">&gt;</span><span class="hl-code">
    </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">a</span><span class="hl-brackets">&gt;</span><span class="hl-code"> written by {{ author }} </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">a</span><span class="hl-brackets">&gt;</span><span class="hl-code">
    </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">p</span><span class="hl-brackets">&gt;</span><span class="hl-code"> 感谢 </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">a</span><span class="hl-code"> </span><span class="hl-var">href</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">https://github.com/showonne</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">showonne</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">a</span><span class="hl-brackets">&gt;</span><span class="hl-code">大神的技术指导</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">p</span><span class="hl-brackets">&gt;</span><span class="hl-code">
  </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">div</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">template</span><span class="hl-brackets">&gt;</span><span class="hl-code">
 
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span><span class="hl-code">
export default {
  data() {
    return {
      author: &quot;微信公众号 jinkey-love&quot;,
      articles: [],
    }
  }
  }
}
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span><span class="hl-code">
 
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">style</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">style</span><span class="hl-brackets">&gt;</span></div>
</div>
</div>
<p>这时候修改 main.js，引入并注册 <code>vue-router</code></p>
<pre>
import VueRouter from "vue-router";
Vue.use(VueRouter);
</pre><p>并且配置路由规则和 app 启动配置项加上 router，旧版的 router.map 方法在 <code>vue-router 2.0</code> 已经不能用了。修改后的<code>main.js</code>如下:</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">Vue</span><span class="hl-code"> </span><span class="hl-identifier">from</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">vue</span><span class="hl-quotes">'</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">App</span><span class="hl-code"> </span><span class="hl-identifier">from</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">./App.vue</span><span class="hl-quotes">'</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">VueRouter</span><span class="hl-code"> </span><span class="hl-identifier">from</span><span class="hl-code"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">vue-router</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">VueResource</span><span class="hl-code"> </span><span class="hl-identifier">from</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">vue-resource</span><span class="hl-quotes">'</span><span class="hl-code">
 
</span><span class="hl-comment">//</span><span class="hl-comment">开启debug模式</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">Vue</span><span class="hl-code">.</span><span class="hl-identifier">config</span><span class="hl-code">.</span><span class="hl-identifier">debug</span><span class="hl-code"> = </span><span class="hl-reserved">true</span><span class="hl-code">;
 
</span><span class="hl-identifier">Vue</span><span class="hl-code">.</span><span class="hl-identifier">use</span><span class="hl-brackets">(</span><span class="hl-identifier">VueRouter</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">Vue</span><span class="hl-code">.</span><span class="hl-identifier">use</span><span class="hl-brackets">(</span><span class="hl-identifier">VueResource</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> 定义组件, 也可以像教程之前教的方法从别的文件引入</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">const</span><span class="hl-code"> </span><span class="hl-identifier">First</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">template</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">&lt;div&gt;&lt;h2&gt;我是第 1 个子页面&lt;/h2&gt;&lt;/div&gt;</span><span class="hl-quotes">'</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">secondcomponent</span><span class="hl-code"> </span><span class="hl-identifier">from</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">./component/secondcomponent.vue</span><span class="hl-quotes">'</span><span class="hl-code">
 
</span><span class="hl-comment">//</span><span class="hl-comment"> 创建一个路由器实例</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 并且配置路由规则</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">const</span><span class="hl-code"> </span><span class="hl-identifier">router</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">VueRouter</span><span class="hl-brackets">(</span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-identifier">mode</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">history</span><span class="hl-quotes">'</span><span class="hl-code">,
  </span><span class="hl-identifier">base</span><span class="hl-code">: </span><span class="hl-identifier">__dirname</span><span class="hl-code">,
  </span><span class="hl-identifier">routes</span><span class="hl-code">: </span><span class="hl-brackets">[</span><span class="hl-code">
    </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">path</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">/first</span><span class="hl-quotes">'</span><span class="hl-code">,
      </span><span class="hl-identifier">component</span><span class="hl-code">: </span><span class="hl-identifier">First</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">,
    </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">path</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">/second</span><span class="hl-quotes">'</span><span class="hl-code">,
      </span><span class="hl-identifier">component</span><span class="hl-code">: </span><span class="hl-identifier">secondcomponent</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
  </span><span class="hl-brackets">]</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-comment">//</span><span class="hl-comment"> 现在我们可以启动应用了！</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 路由器会创建一个 App 实例，并且挂载到选择符 #app 匹配的元素上。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">const</span><span class="hl-code"> </span><span class="hl-identifier">app</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Vue</span><span class="hl-brackets">(</span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-identifier">router</span><span class="hl-code">: </span><span class="hl-identifier">router</span><span class="hl-code">,
  </span><span class="hl-identifier">render</span><span class="hl-code">: </span><span class="hl-identifier">h</span><span class="hl-code"> =&gt; </span><span class="hl-identifier">h</span><span class="hl-brackets">(</span><span class="hl-identifier">App</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.$</span><span class="hl-identifier">mount</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">#app</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>这样子改完再打开浏览器看看。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691880-5461-854231-cf956a57f849e8c3.png"></p>
<p>点击那两个链接试试，会发现<code>&lt;router-view class="view"&gt;&lt;/router-view&gt;</code>的内容已经展示出来，同时注意<strong>浏览器地址已经变更</strong>。<br></p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691907-2400-854231-958ea2c6a5933db6.png"></p>
<p>另外，也可以把 App.vue 的内容写在 main.js 也是可以的不过不建议这么做</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476691937-2753-854231-6139d6fbb1cbcb69.jpg"></p><p>如果你使用 vue1.0和0.7版本的 vue-router，请参照下面这个教程, 他整个系列都不错的，当然仅限于 vue1.0 :</p>
<pre>
http://guowenfh.github.io/2016/03/28/vue-webpack-06-router/
</pre>
<hr>
<h2>给页面加点动态数据</h2><p>这时候的页面都是静态的(数据在写程序的时候已经固定了不能修改)，而每个应用基本上都会请求外部数据以动态改变页面内容。对应有一个库叫 <code>vue-resource</code> 帮我们解决这个问题。<br>使用命令行安装</p>
<pre>cnpm install vue-resource --save</pre><p>在 main.js 引入并注册 <code>vue-resource</code>:</p>
<pre>import VueResource from 'vue-resource'
Vue.use(VueResource);</pre><p>我们在 secondcomponent.vue 上来动态加载数据<br>添加一个列表:</p>
<pre>
&lt;ul&gt;
  &lt;li v-for="article in articles"&gt;
    {{article.title}}
  &lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>在 data 里面加入数组 articles 并赋值为[]<br>然后在 data 后面加入加入钩子函数 <code>mounted</code>(详细请参照官方文档关于 vue 生命周期的解析)，<strong>data 和 mount 中间记得记得加逗号</strong></p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-identifier">mounted</span><span class="hl-code">: </span><span class="hl-reserved">function</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">this</span><span class="hl-code">.$</span><span class="hl-identifier">http</span><span class="hl-code">.</span><span class="hl-identifier">jsonp</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">https://api.douban.com/v2/movie/top250?count=10</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">headers</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
 
        </span><span class="hl-brackets">}</span><span class="hl-code">,
        </span><span class="hl-identifier">emulateJSON</span><span class="hl-code">: </span><span class="hl-reserved">true</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">then</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-brackets">(</span><span class="hl-identifier">response</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-comment">//</span><span class="hl-comment"> 这里是处理正确的回调</span><span class="hl-comment"></span><span class="hl-code">
 
        </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">articles</span><span class="hl-code"> = </span><span class="hl-identifier">response</span><span class="hl-code">.</span><span class="hl-identifier">data</span><span class="hl-code">.</span><span class="hl-identifier">subjects</span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> this.articles = response.data[&quot;subjects&quot;] 也可以</span><span class="hl-comment"></span><span class="hl-code">
 
    </span><span class="hl-brackets">}</span><span class="hl-code">, </span><span class="hl-reserved">function</span><span class="hl-brackets">(</span><span class="hl-identifier">response</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> 这里是处理错误的回调</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">console</span><span class="hl-code">.</span><span class="hl-identifier">log</span><span class="hl-brackets">(</span><span class="hl-identifier">response</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>这里使用的是豆瓣的公开 GET 接口，如果接口是跨域的 POST 请求，则需要在服务器端配置:</p>
<pre>Access-Control-Allow-Origin: *</pre>
<p>这时候运行看看。等一会接口返回数据，咦，数据加载出来了，棒棒哒 !<br></p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476692090-4577-854231-4871af2f1af20ca5.png"></p>
<p>更多 vue-router 的使用方法可以看

</p>
<blockquote><p><strong>vue-router 2.0</strong><br><a href="http://router.vuejs.org/zh-cn/index.html" target="_blank" rel="noopener">http://router.vuejs.org/zh-cn/index.html</a></p></blockquote>
<hr>
<h2>来拯救如此难看的界面</h2>
<p>组件、双向绑定、路由、数据请求等基本特性都能用了，写到这里一个单页应用基本上成型了。但是，这界面也太 TM 难看了吧。自己写 UI 框架太费劲？那就上网找一个吧。<br>本来想给大家介绍 Vux 的，因为他用的是微信的 WeUI 设计规范，对于开发微信小程序或者微信内的网页非常和谐，但由于写这篇文章的时候 Vux 还不支持 vue2.0，只能用别的框架了。<br>命令行安装 ElementUI (此处某公司的人应该发红包了...)</p>
<pre>
cnpm install element-ui@next -S
</pre><p>然后在 main.js 引入并注册</p>
<pre>
import Element from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
Vue.use(Element)
</pre>
<p>保存，这时候程序报错</p>
<pre>Uncaught Error: Module parse failed: /Users/**/Desktop/vue2/node_modules/.1.0.0-rc.5@element-ui/lib/theme-default/index.css Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type.</pre>
<p>官网文档又有坑了，安装教程也不跟我们说这一步，当我们都是高手了...<br>报错是由于我们引入了<code>index.css</code>这个 CSS 文件，但是 webpack 打包的时候无法识别并转换成 js，所以就需要配置才能读取 css 和字体文件，运行命令安装下面三个东西(如果之前安装过就不需要了)</p>
<pre>
cnpm install style-loader --save-dev
cnpm install css-loader --save-dev
cnpm install file-loader --save-dev
</pre>
<p>在 webpack.config.js 中的 loaders 数组加入以下配置，记得该加逗号的地方加逗号!</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-identifier">css</span><span class="hl-code">$/,
    </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">&quot;</span><span class="hl-string">style!css</span><span class="hl-quotes">&quot;</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">,
</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-brackets">(</span><span class="hl-identifier">eot</span><span class="hl-code">|</span><span class="hl-identifier">woff</span><span class="hl-code">|</span><span class="hl-identifier">woff2</span><span class="hl-code">|</span><span class="hl-identifier">ttf</span><span class="hl-brackets">)</span><span class="hl-brackets">(</span><span class="hl-brackets">[</span><span class="hl-code">\\\\\\\\?</span><span class="hl-brackets">]</span><span class="hl-code">?.*</span><span class="hl-brackets">)</span><span class="hl-code">$/,
    </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">&quot;</span><span class="hl-string">file</span><span class="hl-quotes">&quot;</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>修改完的 webpack.config.js 如下</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">path</span><span class="hl-code"> = </span><span class="hl-identifier">require</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">path</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">webpack</span><span class="hl-code"> = </span><span class="hl-identifier">require</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">webpack</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-identifier">module</span><span class="hl-code">.</span><span class="hl-identifier">exports</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-identifier">entry</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">./src/main.js</span><span class="hl-quotes">'</span><span class="hl-code">,
  </span><span class="hl-identifier">output</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">path</span><span class="hl-code">: </span><span class="hl-identifier">path</span><span class="hl-code">.</span><span class="hl-identifier">resolve</span><span class="hl-brackets">(</span><span class="hl-identifier">__dirname</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">./dist</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">,
    </span><span class="hl-identifier">publicPath</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">/dist/</span><span class="hl-quotes">'</span><span class="hl-code">,
    </span><span class="hl-identifier">filename</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">build.js</span><span class="hl-quotes">'</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">resolveLoader</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">root</span><span class="hl-code">: </span><span class="hl-identifier">path</span><span class="hl-code">.</span><span class="hl-identifier">join</span><span class="hl-brackets">(</span><span class="hl-identifier">__dirname</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">node_modules</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">,
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">module</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">loaders</span><span class="hl-code">: </span><span class="hl-brackets">[</span><span class="hl-code">
      </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-identifier">vue</span><span class="hl-code">$/,
        </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">vue</span><span class="hl-quotes">'</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">,
      </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-identifier">js</span><span class="hl-code">$/,
        </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">babel</span><span class="hl-quotes">'</span><span class="hl-code">,
        </span><span class="hl-identifier">exclude</span><span class="hl-code">: /</span><span class="hl-identifier">node_modules</span><span class="hl-code">/
      </span><span class="hl-brackets">}</span><span class="hl-code">,
      </span><span class="hl-brackets">{</span><span class="hl-code">
          </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-identifier">css</span><span class="hl-code">$/,
          </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">&quot;</span><span class="hl-string">style!css</span><span class="hl-quotes">&quot;</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">,
      </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-brackets">(</span><span class="hl-identifier">eot</span><span class="hl-code">|</span><span class="hl-identifier">woff</span><span class="hl-code">|</span><span class="hl-identifier">woff2</span><span class="hl-code">|</span><span class="hl-identifier">ttf</span><span class="hl-brackets">)</span><span class="hl-brackets">(</span><span class="hl-brackets">[</span><span class="hl-code">\\\\\\\\?</span><span class="hl-brackets">]</span><span class="hl-code">?.*</span><span class="hl-brackets">)</span><span class="hl-code">$/,
        </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">&quot;</span><span class="hl-string">file</span><span class="hl-quotes">&quot;</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">,
      </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">test</span><span class="hl-code">: /\\\\\\\\.</span><span class="hl-brackets">(</span><span class="hl-identifier">png</span><span class="hl-code">|</span><span class="hl-identifier">jpg</span><span class="hl-code">|</span><span class="hl-identifier">gif</span><span class="hl-code">|</span><span class="hl-identifier">svg</span><span class="hl-brackets">)</span><span class="hl-code">$/,
        </span><span class="hl-identifier">loader</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">file</span><span class="hl-quotes">'</span><span class="hl-code">,
        </span><span class="hl-identifier">query</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
          </span><span class="hl-identifier">name</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">[name].[ext]?[hash]</span><span class="hl-quotes">'</span><span class="hl-code">
        </span><span class="hl-brackets">}</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-brackets">]</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">resolve</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">alias</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-identifier">vue</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">vue/dist/vue.js</span><span class="hl-quotes">'</span><span class="hl-brackets">}</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">devServer</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">historyApiFallback</span><span class="hl-code">: </span><span class="hl-reserved">true</span><span class="hl-code">,
    </span><span class="hl-identifier">noInfo</span><span class="hl-code">: </span><span class="hl-reserved">true</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">,
  </span><span class="hl-identifier">devtool</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">#eval-source-map</span><span class="hl-quotes">'</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">process</span><span class="hl-code">.</span><span class="hl-identifier">env</span><span class="hl-code">.</span><span class="hl-identifier">NODE_ENV</span><span class="hl-code"> === </span><span class="hl-quotes">'</span><span class="hl-string">production</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-identifier">module</span><span class="hl-code">.</span><span class="hl-identifier">exports</span><span class="hl-code">.</span><span class="hl-identifier">devtool</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">#source-map</span><span class="hl-quotes">'</span><span class="hl-code">
  </span><span class="hl-comment">//</span><span class="hl-comment"> </span><span class="hl-url">http://vue-loader.vuejs.org/en/workflow/production.html</span><span class="hl-comment"></span><span class="hl-code">
  </span><span class="hl-identifier">module</span><span class="hl-code">.</span><span class="hl-identifier">exports</span><span class="hl-code">.</span><span class="hl-identifier">plugins</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">module</span><span class="hl-code">.</span><span class="hl-identifier">exports</span><span class="hl-code">.</span><span class="hl-identifier">plugins</span><span class="hl-code"> || </span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">concat</span><span class="hl-brackets">(</span><span class="hl-brackets">[</span><span class="hl-code">
    </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">webpack</span><span class="hl-code">.</span><span class="hl-identifier">DefinePlugin</span><span class="hl-brackets">(</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-quotes">'</span><span class="hl-string">process.env</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">NODE_ENV</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">&quot;production&quot;</span><span class="hl-quotes">'</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">,
    </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">webpack</span><span class="hl-code">.</span><span class="hl-identifier">optimize</span><span class="hl-code">.</span><span class="hl-identifier">UglifyJsPlugin</span><span class="hl-brackets">(</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">compress</span><span class="hl-code">: </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">warnings</span><span class="hl-code">: </span><span class="hl-reserved">false</span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">
  </span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>给豆瓣的电影列表套个衣服(样式) :</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">el-card</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">box-card</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
      </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">div</span><span class="hl-code"> </span><span class="hl-var">slot</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">header</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">clearfix</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
        </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">h2</span><span class="hl-code"> </span><span class="hl-var">style</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">line-height: 36px; color: #20A0FF</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">豆瓣电影排行榜</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">h2</span><span class="hl-brackets">&gt;</span><span class="hl-code">
      </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">div</span><span class="hl-brackets">&gt;</span><span class="hl-code">
      </span><span class="hl-brackets">&lt;</span><span class="hl-reserved">div</span><span class="hl-code"> </span><span class="hl-var">v-for</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">article in articles</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">class</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">text item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
        {{article.title}}
      </span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">div</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">el-card</span><span class="hl-brackets">&gt;</span></div>
</div>
</div>
<p>打开浏览器,输入网址:http://localhost:8080/second</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2016/10/1476692273-6825-854231-0f8758be535e7ccd.png"></p>
<p>列表比之前漂亮多了，你还可以参照 ElementUI 的文档使用更多组件样式</p>
<blockquote><p><a href="http://element.eleme.io/#/component/layout" target="_blank" rel="noopener">http://element.eleme.io/#/component/layout</a></p></blockquote>
h3>编译</h3>
<pre>npm run build</pre>
<p>又报错了...orz</p>
<pre>
ERROR in build.js from UglifyJs
SyntaxError: Unexpected token punc &laquo;(&raquo;, expected punc &laquo;:&raquo; [build.js:32001,6]
把node_modules/.bin/cross-env里的
require('../dist')(process.argv.slice(2));
</pre>
<p>后来发现直接运行 webpack 命令就可以打包了</p>
<pre>webpack --color --progress</pre>
<p>接着把 index.html 和整个 dist 目录丢到服务器就可以了。</p>				</div>
			</div>
			<div class="previous-next-links">
			<div class="previous-design-link">← <a href="../w3cnote/centos-mail-command-sendmail.html" rel="prev"> Linux 使用命令发送邮件</a> </div>
			<div class="next-design-link"><a href="../w3cnote/wx-xcx-repo.html" rel="next"> 微信小程序开发资源汇总</a> →</div>
			</div>
			<style>
.wrapper {
  /*text-transform: uppercase; */
  background: #ececec;
  color: #555;
  cursor: help;
  font-family: "Gill Sans", Impact, sans-serif;
  font-size: 20px;
  position: relative;
  text-align: center;
  width: 200px;
  -webkit-transform: translateZ(0); /* webkit flicker fix */
  -webkit-font-smoothing: antialiased; /* webkit text rendering fix */
}

.wrapper .tooltip {
  white-space: nowrap;
  font-size: 14px;
  text-align: left;
  background: #96b97d;
  bottom: 100%;
  color: #fff;
  display: block;
  left: -25px;
  margin-bottom: 15px;
  opacity: 0;
  padding: 14px;
  pointer-events: none;
  position: absolute;
  
  -webkit-transform: translateY(10px);
     -moz-transform: translateY(10px);
      -ms-transform: translateY(10px);
       -o-transform: translateY(10px);
          transform: translateY(10px);
  -webkit-transition: all .25s ease-out;
     -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
       -o-transition: all .25s ease-out;
          transition: all .25s ease-out;
  -webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
     -moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
      -ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
       -o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
          box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
.tooltip a {
	color:#fff;
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
  bottom: -20px;
  content: " ";
  display: block;
  height: 20px;
  left: 0;
  position: absolute;
  width: 100%;
}  

/* CSS Triangles - see Trevor's post */
.wrapper .tooltip:after {
  border-left: solid transparent 10px;
  border-right: solid transparent 10px;
  border-top: solid #96b97d 10px;
  bottom: -10px;
  content: " ";
  height: 0;
  left: 20%;
  margin-left: -13px;
  position: absolute;
  width: 0;
}
.wrapper .tooltip1 {
	margin-left: 50px;
	padding-top: 0px;
}
/*
.wrapper:hover .tooltip {
  opacity: 1;
  pointer-events: auto;
  -webkit-transform: translateY(0px);
     -moz-transform: translateY(0px);
      -ms-transform: translateY(0px);
       -o-transform: translateY(0px);
          transform: translateY(0px);
}
*/
/* IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
  display: none;
}

.lte8 .wrapper:hover .tooltip {
  display: block;
}

</style>

<link rel="stylesheet" href="../0/upvotejs.css">
<script src=""></script>
<script src=""></script>
<div id="respond" class="no_webshot"> 
		<div class="comment-signarea" style=" padding: 20px 20px;"> 
	<h3 class="text-muted" id="share_code" style="color: #799961;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> 点我分享笔记</h3>
	<!--
	<p style="font-size:14px;">笔记需要是本篇文章的内容扩展！</p><br>
	<p style="font-size:12px;"><a href="../tougao" target="_blank">文章投稿，可点击这里</a></p>
	<p style="font-size:14px;"><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>
		<h3 class="text-muted"><i class="fa fa-info-circle" aria-hidden="true"></i> 分享笔记前必须<a href="javascript:;" class="runoob-pop">登录</a>！</h3>
		<p><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>-->
	</div>
		
	<form action="/wp-content/themes/runoob/option/addnote.php" method="post" id="commentform" style="display:none;">
		<div class="comt">
			<div class="comt-title">
				<i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i>				<p><a id="cancel-comment-reply-link" href="javascript:;">取消</a></p>
			</div>
			<div class="comt-box">
			<div id="mded"></div>
			
				<div class="comt-ctrl">
					<div class="comt-tips"><input type='hidden' name='comment_post_ID' value='16905' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</div>
					<button type="submit" name="submit" id="submit" tabindex="5"><i class="fa fa-pencil" aria-hidden="true"></i> 分享笔记</button>
				</div>
			</div>
		
				
					<div class="comt-comterinfo"> 
						<ul id="comment-author-info">
							<li class="form-inline"><label class="hide" for="author">昵称</label><input class="ipt" type="text" name="author" id="author" value="" tabindex="2" placeholder="昵称"><span class="text-muted">昵称 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="email">邮箱</label><input class="ipt" type="text" name="email" id="email" value="" tabindex="3" placeholder="邮箱"><span class="text-muted">邮箱 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="url">引用地址</label><input class="ipt" type="text" name="url" id="url" value="" tabindex="4" placeholder="引用地址"><span class="text-muted">引用地址</span></li>
						</ul>
					</div>
				
			
		</div>

	</form>
	</div>
<script type="text/javascript">
$(function() {
	//初始化编辑器
	
	var editor = new Simditor({
	  textarea: $('#mded'),
	  placeholder: '写笔记...',
	  upload:false,
	 // upload: {url:'/api/comment_upload_file.php',params: null,fileKey: 'upload_file',connectionCount: 1,leaveConfirm: '文件正在上传，您确定离开?'},
	  defaultImage: 'https://www.runoob.com/images/logo.png',
	  codeLanguages: '',
	  autosave: 'editor-content',
	  toolbar: [  'bold','code','ul','ol','image' ]
	});
	editor.on('selectionchanged', function() {
		$(".code-popover").hide();
	});

	// 提交数据
	$("#share_code").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		
	});
	$("#user_add_note").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		$('html, body').animate({
       	    scrollTop: $("#respond").offset().top
    	}, 200);
	});

	// 提交笔记
	var commentform=$('#commentform');
	commentform.prepend('<div id="comment-status" style="display:none;" ></div>');
	var statusdiv=$('#comment-status');
	
	commentform.submit(function(e){
		e.preventDefault();
		var noteContent = editor.getValue();
		// console.log(noteContent);
		noteContent = noteContent.replace(/<pre><code>/g,"<pre>");
		noteContent = noteContent.replace(/<\/code><\/pre>/g,"</pre>");
		
		// 系列化表单数据
		var comment_parent = 0;
		var is_user_logged_in = $("#is_user_logged_in").val();
		var comment_post_ID =  16905;
		var _wp_unfiltered_html_comment = $("#_wp_unfiltered_html_comment").val();
		var comment = noteContent;
		var author = $("#author").val();
		var url = $("#url").val();
		var email = $("#email").val();
		if(isBlank(author) && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入昵称！</p>').show();
		} else if(isBlank(email)  && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入邮箱！</p>').show();
		} else {
			// var formdata=commentform.serialize() + "&comment=" + noteContent ;
			// 添加状态信息
			statusdiv.html('<p>Processing...</p>').show();
			// 获取表单提交地址
			var formurl=commentform.attr('action');
			
			// 异步提交
			$.ajax({
					type: 'post',
					url: formurl,
					dataType:'json',
					data: {"comment_parent":comment_parent,"comment_post_ID":comment_post_ID, "_wp_unfiltered_html_comment":_wp_unfiltered_html_comment,"comment":comment,"url":url, "email":email,"author":author},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					statusdiv.html('<p class="ajax-error" >数据不完整或表单提交太快了！</p>').show();
				},
				success: function(data, textStatus){
					if(data.errorno=="0") {
						$("#submit").prop('disabled', true);
						statusdiv.html('<p class="ajax-success" >笔记已提交审核，感谢分享笔记！</p>').show();
						alert('笔记已提交审核，感谢分享笔记！');
					}else{
						statusdiv.html('<p class="ajax-error" >'+data.msg+'</p>').show();
					}
					commentform.find('textarea[name=comment]').val('');
				}
			});
			setTimeout(function(){
		        $("#submit").prop('disabled', false);
		    }, 10*1000);
		}
		return false;

	});
	$(".comt-author").click(function() {
		href = $(this).children("a").attr("href");
		if(href.indexOf("/note/")!=-1) {
			var win = window.open(href, '_blank');
  			win.focus();
		}
	});
	$(".comt-meta span").hover(function(){
		$(this).children(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).children(".tooltip").removeAttr("style");
	});
	/*
	$(".wrapper i").hover(function(){
		$(this).siblings(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).siblings(".tooltip").css({ "opacity": 0, "pointer-events": "auto"});
	});
	*/
	//Upvote.create('runoobvote-id', {callback: vote_callback});
	var ajaxurl = 'https://www.runoob.com/wp-admin/admin-ajax.php';
	var callback = function(data) {
		//console.log($('#runoobvote-id').upvote('upvoted'));
		//console.log($('#runoobvote-id').upvote('downvoted'));
		//console.log(data);
		_vote_action = data.action;
		id_arr = data.id.split('-');
		um_id= id_arr[2];
		//console.log(um_id);
		
		var re = /^[1-9]+/;
		if (re.test(um_id)) { 
			var ajax_data = {
				_vote_action: _vote_action,
				action: "pinglun_zan",
				um_id: um_id,
				um_action: "ding"
			};
			//console.log(ajax_data);
			$.post(ajaxurl,ajax_data,function(status){
				//if(status.vote_num>999) {
				//	_voteHtml = '<span style="display: block; text-align: center;font-size: 20px; color: #6a737c; margin: 8px 0;">'+kFormatter(status.vote_num) +'</span>';
				//	$("#runoobvote-id-" + um_id + " .count").hide().after(_voteHtml);
				//}
				
			});
		}
	};
	if($('#comments').length && $('.upvotejs').length){
		$('.upvotejs').upvote({id: 16905, callback: callback});
	
		$.post(ajaxurl,{"action":"pinglun_zan","postid":16905},function(data){  
			$(data).each(function(key,value) {
				$("#runoobvote-id-" + value.commid + " .upvote").addClass(value.upvotejs_class);
				$("#runoobvote-id-" + value.commid + " .downvote").addClass(value.downvote_class);
				$("#runoobvote-id-" + value.commid + " .count").text(value.upvote_count);
			})
		},'json');
		
	}
	
	
});
function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}
function kFormatter(num) {
	// return num;
    return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}

</script>

<link rel="stylesheet" href="../0/qa.css?1.44">
<link rel="stylesheet" type="text/css" href="../0/simditor.min.css" />
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
			<div class="article-heading-ad" id="w3cnote-ad728">
			<script async src=""></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="horizontal"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
			</div>
			<style>
@media screen and (max-width: 768px) {
	#w3cnote-ad728 {
		display: none;
	}
}
p.note-author {
    border-bottom: 1px solid #ddd;
    font-size: 18px;
    font-weight: bold;
    color: #78a15a;
    padding-bottom: 2px;
    margin-bottom: 4px;
}
</style>
<script>
var aid = 16905;
</script>
	</div>
		
	</div>
	<div class="listcol last right-column">




<!--
	<div class="tab tab-light-blue"> 订阅</div>
	<div class="sidebar-box">
		<div class="socialicons">
			<a href="../feed" class="rss">RSS 订阅</a>
		
			<form action="//list.qq.com/cgi-bin/qf_compose_send" method="post">
			<input type="hidden" value="qf_booked_feedback" name="t">
			<input type="hidden" value="4b67b6b6c1f5e792559940cab4aebb8f1126fba880bff1a8" name="id">
			<input class="placeholder" id="feed_email" name="to" value="输入邮箱 订阅笔记" autocomplete="off">
			<input type="submit" value="订阅" class="btn btn-primary">
			</form>
		
		</div>
 
	</div>
-->	



<!--
	<div class="sidebar-box cate-list">
	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">笔记列表</a>
		</div>

 

</div>
-->

	 <div class="sidebar-box cate-list">
		 		

	 	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">教程列表</a>
		</div>
		
		<div class="cate-items"> 
				<a href="../ado">ADO 教程</a>
	<a href="../ajax">Ajax 教程</a>
	<a href="../android">Android 教程</a>
	<a href="../angularjs2">Angular2 教程</a>
	<a href="../angularjs">AngularJS 教程</a>
	<a href="../appml">AppML 教程</a>
	<a href="../asp">ASP 教程</a>
	<a href="../aspnet">ASP.NET 教程</a>
	<a href="../bootstrap">Bootstrap 教程</a>
	<a href="../bootstrap4">Bootstrap4 教程</a>
	<a href="../bootstrap5">Bootstrap5 教程</a>
	<a href="../cprogramming">C 教程</a>
	<a href="../csharp">C# 教程</a>
	<a href="../cplusplus">C++ 教程</a>
	<a href="../chartjs">Chart.js 教程</a>
	<a href="../cssref">CSS 参考手册</a>
	<a href="../css">CSS 教程</a>
	<a href="../css3">CSS3 教程</a>
	<a href="../django">Django 教程</a>
	<a href="../docker">Docker 教程</a>
	<a href="../dtd">DTD 教程</a>
	<a href="../echarts">ECharts 教程</a>
	<a href="../eclipse">Eclipse 教程</a>
	<a href="../firebug">Firebug 教程</a>
	<a href="../font-awesome">Font Awesome 图标</a>
	<a href="../foundation">Foundation 教程</a>
	<a href="../git">Git 教程</a>
	<a href="../go">Go 语言教程</a>
	<a href="../googleapi">Google 地图 API 教程</a>
	<a href="../highcharts">Highcharts 教程</a>
	<a href="../htmldom">HTML DOM 教程</a>
	<a href="../tags">HTML 参考手册</a>
	<a href="../charsets">HTML 字符集</a>
	<a href="../html">HTML 教程</a>
	<a href="../http">HTTP 教程</a>
	<a href="../ionic">ionic 教程</a>
	<a href="../ios">iOS 教程</a>
	<a href="../java">Java 教程</a>
	<a href="../jsref">JavaScript 参考手册</a>
	<a href="../js">Javascript 教程</a>
	<a href="../jeasyui">jQuery EasyUI 教程</a>
	<a href="../jquerymobile">jQuery Mobile 教程</a>
	<a href="../jqueryui">jQuery UI 教程</a>
	<a href="../jquery">jQuery 教程</a>
	<a href="../json">JSON 教程</a>
	<a href="../jsp">JSP 教程</a>
	<a href="../julia">Julia 教程</a>
	<a href="../kotlin">Kotlin 教程</a>
	<a href="../linux">Linux 教程</a>
	<a href="../lua">Lua 教程</a>
	<a href="../markdown">Markdown 教程</a>
	<a href="../matplotlib">Matplotlib 教程</a>
	<a href="../maven">Maven 教程</a>
	<a href="../memcached">Memcached 教程</a>
	<a href="../mongodb">MongoDB 教程</a>
	<a href="../mysql">MySQL 教程</a>
	<a href="../nodejs">Node.js 教程</a>
	<a href="../numpy">NumPy 教程</a>
	<a href="../pandas">Pandas 教程</a>
	<a href="../perl">Perl 教程</a>
	<a href="../php">PHP 教程</a>
	<a href="../postgresql">PostgreSQL 教程</a>
	<a href="../python3">Python 3 教程</a>
	<a href="../python">Python 基础教程</a>
	<a href="../r">R 教程</a>
	<a href="../rdf">RDF 教程</a>
	<a href="../react">React 教程</a>
	<a href="../redis">Redis 教程</a>
	<a href="../rss">RSS 教程</a>
	<a href="../ruby">Ruby 教程</a>
	<a href="../rust">Rust 教程</a>
	<a href="../sass">Sass 教程</a>
	<a href="../scala">Scala 教程</a>
	<a href="../scipy">SciPy 教程</a>
	<a href="../servlet">Servlet 教程</a>
	<a href="../soap">SOAP 教程</a>
	<a href="../sql">SQL 教程</a>
	<a href="../sqlite">SQLite 教程</a>
	<a href="../svg">SVG 教程</a>
	<a href="../svn">SVN 教程</a>
	<a href="../swift">Swift 教程</a>
	<a href="../tcpip">TCP/IP 教程</a>
	<a href="../typescript">TypeScript 教程</a>
	<a href="../vbscript">VBScript 教程</a>
	<a href="../vue2">Vue.js 教程</a>
	<a href="../vue3">Vue3 教程</a>
	<a href="../w3c">W3C 教程</a>
	<a href="../webservices">Web Service 教程</a>
	<a href="../wsdl">WSDL 教程</a>
	<a href="../xlink">XLink 教程</a>
	<a href="../dom">XML DOM 教程</a>
	<a href="../schema">XML Schema 教程</a>
	<a href="../xml">XML 教程</a>
	<a href="../xpath">XPath 教程</a>
	<a href="../xquery">XQuery 教程</a>
	<a href="../xslfo">XSLFO 教程</a>
	<a href="../xsl">XSLT 教程</a>
	<a href="../data-structures">数据结构</a>
	<a href="../regexp">正则表达式</a>
	<a href="../quiz">测验</a>
	<a href="../browsers">浏览器</a>
	<a href="../quality">网站品质</a>
	<a href="../web">网站建设指南</a>
	<a href="../hosting">网站服务器教程</a>
	<a href="../design-pattern">设计模式</a>
			
		</div> 
		 	 </div>
</div>
	</div>
</div>


<!-- 底部 -->
<div id="footer" class="mar-t50">
   <div class="runoob-block">
    <div class="runoob cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a target="_blank" href="../html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a target="_blank" href="../jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a target="_blank" href="../charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6939/">JS 混淆/加密</a>
      </dd> 
      <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6232/">PNG/JPEG 图片压缩</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/53">JSON 格式化工具</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/6680/">随机数生成器</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imread.html" title="Matplotlib imread() 方法">Matplotlib imre...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imsave.html" title="Matplotlib imsave() 方法">Matplotlib imsa...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imshow.html" title="Matplotlib imshow() 方法">Matplotlib imsh...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-hist.html" title="Matplotlib 直方图">Matplotlib 直方图</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-func-object.html" title="Python object() 函数">Python object()...</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-ai-draw.html" title="Python AI 绘画">Python AI 绘画</a>
      </dd>
              <dd>
       &middot;
      <a href="../w3cnote/cursor-editor.html" title="神辅助 Cursor 编辑器，加入 GPT-4 让编码更轻松！">神辅助 Cursor ...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="mailto:admin@runoob.com" rel="external nofollow">意见反馈</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a target="_blank" href="../aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../archives">文章归档</a>
      </dd>
    
     </dl>
    
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png" />
       </div>
     </div>
     
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2023    <strong><a href="../" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="../" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/">闽ICP备15012807号-1</a>
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" style="display:none" href="javascript:void(0)"   title="标记/收藏"><i class="fa fa-star" aria-hidden="true"></i></a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png"></div> 
    </div>
  </div>

 <div style="display:none;">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-84264393-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-84264393-2');
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3eec0b7da6548cf07db3bc477ea905ee";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</div>
<script>
window.jsui={
    www: 'https://www.runoob.com',
    uri: 'https://www.runoob.com/wp-content/themes/runoob'
};
</script>

<script src=""></script>
<script src=""></script>

</body>
</html>